package com.dycx.api.controller.manage.system;

import com.dycx.common.RestResponse;
import com.dycx.common.annotation.WriterLog;
import com.dycx.common.enums.BizTypeEnum;
import com.dycx.system.dto.PermissionDTO;
import com.dycx.system.service.PermissionService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;


@RestController
@RequestMapping(value = "/manage/system/permission")
public class PermissionController {

    @Resource
    private PermissionService permissionService;

    /**
     * 添加
     **/
    @WriterLog(bizType = BizTypeEnum.SYS_PERMISSION, bizValue = "{#dto.id}", logTitle = "添加权限")
    @PostMapping("/add")
    public RestResponse<?> add(@RequestBody @Valid PermissionDTO dto) {
        return permissionService.add(dto) ? RestResponse.success() : RestResponse.fail();
    }

    /**
     * 修改
     **/
    @WriterLog(bizType = BizTypeEnum.SYS_PERMISSION, bizValue = "{#dto.id}", logTitle = "更新权限")
    @PostMapping("/edit")
    public RestResponse<?> edit(@RequestBody @Valid PermissionDTO dto) {
        return permissionService.edit(dto) ? RestResponse.success() : RestResponse.fail();
    }

    /**
     * 刪除
     **/
    @WriterLog(bizType = BizTypeEnum.SYS_PERMISSION, bizValue = "{#id}", logTitle = "删除权限")
    @GetMapping("/delete")
    public RestResponse<?> delete(Integer id) {
        return permissionService.delete(id) ? RestResponse.success() : RestResponse.fail();
    }


    /**
     * 查询 根据主键 id 查询
     **/
    @GetMapping("/getAllList")
    public RestResponse<?> getAllList() {
        return RestResponse.success(permissionService.getAllList());
    }

    /**
     * 修改排序
     **/
    @WriterLog(bizType = BizTypeEnum.SYS_PERMISSION, bizValue = "{#dto.id}", logTitle = "权限排序")
    @PostMapping("/updateSort")
    public RestResponse<?> updateSort(@RequestBody PermissionDTO dto) {
        return RestResponse.success(permissionService.updateSort(dto));
    }

    /**
     * 修改状态
     **/
    @WriterLog(bizType = BizTypeEnum.SYS_PERMISSION, bizValue = "{#dto.id}", logTitle = "更新权限状态")
    @PostMapping("/updateStatus")
    public RestResponse<?> updateStatus(@RequestBody PermissionDTO dto) {
        return RestResponse.success(permissionService.updateStatus(dto));
    }

    /**
     * 查询子类
     **/
    @GetMapping("/getListByParentId")
    public RestResponse<?> getListByParentId(Integer parentId) {
        return RestResponse.success(permissionService.getListByParentId(parentId));
    }
}
